const mongoose = require('mongoose');

async function main(){
    await mongoose.connect('mongodb://localhost:27017/local');

    console.log('dataBase connect success')


    // 创建schema
    let person= new mongoose.Schema({
        name:String
    })

    let Person = mongoose.model('person', person)

    let job = new mongoose.Schema({
        nameJob:String,
        user:{
            type:mongoose.Schema.ObjectId,
            ref:Person
        }
    })

    let Job = mongoose.model('job',job)

    let p1 = new Person({
        name:'tom'
    })
    let j1 = new Job({
        nameJob:'javascript',
        user:p1
    })

    p1.save().then(res=>{

        j1.save().then(res=>{
// populate. 通过保存的。mongoose.Schema.ObjectId 来补全另一个文档的某条数据
            Job.findOne().populate('user').then(res=>{
                console.log(res,'====populate')
            })

        })
    })
    
}
main()